package org.campuscontent.io;

import java.io.DataInputStream; 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;


public class PersistenceManager
{
    protected PersistenceManager()
    {
        try
        {
            debug = System.getProperty( "org.campuscontent.persistence.debug" ).equals( "true" );
        }
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
    
    public void save( String save )
    {
        if ( debug )
        {
            try
            {
                System.out.println( "PersistenceManager::save: debugging mode" );
                PrintStream ps = new PrintStream( new File( FILE ) );
                System.out.println(  "PersistenceManager::save >> '"  + save + "'" );
                ps.println( save );
                ps.close();
            }
            catch ( Exception e )
            {
                e.printStackTrace();
            }
            
            return;
        }
        
        if ( save.equals( "" ) || save == null )
        {
            System.out.println( "Cannot publish nothing!" );
        }
        else
        {
            try
            {
                // Create connection to the server
                PHPQuery phpQuery;
                String url = "http://localhost/temp/test.php";

                phpQuery = new PHPQuery( url );

                // set the parameter of query
                phpQuery.add( "query_mode", "2" );
                phpQuery.add( "exercise", save );

                // Get the inputstream from the server

                phpQuery.query();
                System.out.println( save );
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
        }

    }
    
    public DataInputStream load()
    {
        if ( debug )
        {
            try
            {
                System.out.println( "PersistenceManager::load: debugging mode" );
                
                DataInputStream dis = new DataInputStream( new FileInputStream( new File( FILE ) ) );
                return dis;
                
            }
            catch ( Exception e )
            {
            }
            
            return null;
        }
        
        try
        {
            // Create connection to the server
            PHPQuery phpQuery;
            String url = "http://localhost/temp/test.php";
            
            phpQuery = new PHPQuery( url );
            
            // set the parameter of query
            phpQuery.add( "query_mode", "1" );
            
            // Get the inputstream from the server
            
            DataInputStream dis = phpQuery.query();

            return dis;
        }
        catch ( Exception e )
        {
        }
        
        return null;
    }
    
    public static PersistenceManager getInstance()
    {
        if ( self == null )
            self = new PersistenceManager();
        
        return self;
    }
    
    private static PersistenceManager self;
    
    private boolean debug;
    private static final String FILE = "debug.txt"; 
}
